package com.liwy3.common.domain;


import com.liwy3.common.constant.Constants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * Copyright © 元气森林（北京）食品科技集团有限公司. All rights reserved.
 *
 * @Description:
 * @author: Crazy4J
 * @date: 2022年02月24日 15:39
 * @version: V1.0
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, Constants.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, Constants.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, Constants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, Constants.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, Constants.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, Constants.FAIL, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, Constants.FAIL, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
